package mamikeji.liaowa.ui.activity.right.setting;

import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import java.util.HashSet;
import java.util.Set;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;
import mamikeji.liaowa.R;
import mamikeji.liaowa.ui.activity.BaseActivity;
import mamikeji.liaowa.utils.ExampleUtil;
import mamikeji.liaowa.utils.ForContents;
import mamikeji.liaowa.utils.ForPreference;
import mamikeji.liaowa.utils.ForToast;

/**
 * 设置-消息通知开关
 */
public class Set_NewMSGActivity extends BaseActivity {
    private static final String TAG = "Set_NewMSGActivity";
    private ImageView imageView_new_yn;

    private static final int MSG_SET_ALIAS = 1001;
    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(android.os.Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MSG_SET_ALIAS:
                    JPushInterface.setTags(getApplicationContext(),
                            stringSet,
                            mAliasCallback);

                    ForPreference.putBoolean(ForContents.GET_NEW, !ForPreference.getBoolean(ForContents.GET_NEW));
                    imageView_new_yn.setImageResource(ForPreference.getBoolean(ForContents.GET_NEW)? R.drawable.icon_kaiguan: R.drawable.icon_guanbi);
                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set__newmsg);
        getBase();
        setTitleLeftBtn();
        setTitleName(R.string.set_name);
        init();
        initData();
    }

    /**
     * 加载布局控件
     */
    private void init() {
        imageView_new_yn = (ImageView) findViewById(R.id.imageView_new_yn);
        imageView_new_yn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!ForPreference.getBoolean(ForContents.GET_NEW)){
                    getJpushTag(ForPreference.getString(ForContents.APP_JPUSH_TAG));
                }else {
                    getJpushTag("null");
                }
            }
        });
    }

    /**
     * 初始化数据
     */
    private void initData() {
        imageView_new_yn.setImageResource(ForPreference.getBoolean(ForContents.GET_NEW)? R.drawable.icon_kaiqi_n: R.drawable.icon_guanbi_n);
    }

    Set<String> stringSet = new HashSet<>();

    /**
     * 设置用户极光tag
     */
    public void getJpushTag(String tag) {
        String jp_tag = tag;
        if (TextUtils.isEmpty(jp_tag)) {
            ForToast.showToast("1111");
            return;
        }
        if (!ExampleUtil.isValidTagAndAlias(jp_tag)) {
            ForToast.showToast("格式内容error");
            return;
        }
        stringSet.add(jp_tag);
        // 调用 Handler 来异步设置别名
        mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_ALIAS, jp_tag));
    }

    private final TagAliasCallback mAliasCallback = new TagAliasCallback() {
        @Override
        public void gotResult(int code, String jp_tag, Set<String> tags) {
            String logString;
            switch (code) {
                case 0:
                    logString = "Set tag and jp_tag success";
                    break;
                case 6002:
                    logString = "Failed to set jp_tag and tags due to timeout. Try again after 60s.";
                    mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_SET_ALIAS, jp_tag), 0);
                    break;
                default:

                    logString = "Failed with errorCode = " + code;
            }
            ExampleUtil.showToast(logString, getApplicationContext());
        }
    };
}
